package com.ruicar.afs.cloud.basic.proceeds.pay.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicAutoPaymentConfig;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicPaymentBank;
import com.ruicar.afs.cloud.basic.proceeds.pay.mapper.BasicAutoPaymentConfigMapper;
import com.ruicar.afs.cloud.basic.proceeds.pay.service.BasicAutoPaymentConfigService;
import com.ruicar.afs.cloud.basic.proceeds.pay.service.BasicPaymentBankService;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Lee
 * @date 2020-05-30 04:35:07 
 * @description 
 */
@Service
@Slf4j
@AllArgsConstructor
public class BasicAutoPaymentConfigServiceImpl extends ServiceImpl<BasicAutoPaymentConfigMapper, BasicAutoPaymentConfig> implements BasicAutoPaymentConfigService {

    private final BasicPaymentBankService basicPaymentBankService;

    @Override
    public BasicPaymentBank takePayAccount(String bankCode) {

        BasicPaymentBank paymentBank = basicPaymentBankService.getOne(Wrappers.<BasicPaymentBank>query().lambda()
                .eq(BasicPaymentBank::getPaymentBankCode,bankCode)
        );
        if(EmptyUtils.isNotEmpty(paymentBank)){
            return paymentBank;
        }else {
            List<BasicAutoPaymentConfig> autoPaymentConfigList = this.list();
            if (EmptyUtils.isNotEmpty(autoPaymentConfigList)){
                BasicAutoPaymentConfig autoPaymentConfig = autoPaymentConfigList.get(0);
                paymentBank = basicPaymentBankService.getOne(Wrappers.<BasicPaymentBank>query().lambda()
                        .eq(BasicPaymentBank::getPaymentBankCode,autoPaymentConfig.getInterBankTransferBank())
                );
                if(EmptyUtils.isNotEmpty(paymentBank)){
                    return paymentBank;
                }
            }
        }
        return null;
    }
}
